//
//  LoginViewController.m
//  Xiangmu
//
//  Created by 이경인 on 2017/9/25.
//  Copyright © 2017年 LIJINGREN. All rights reserved.
//

#import "LoginViewController.h"
#import "TabBarViewController.h"
#import "ZhuceViewController.h"
#import "AFNetworking.h"
#import "MBProgressHUD/MBProgressHUD.h"
#import "ShouyeViewController.h"



@interface LoginViewController ()

@end

@implementation LoginViewController
//第一次程序加载视图时调用
- (void)viewDidLoad {
    [super viewDidLoad];
   
    //设置背景
    UIImageView *beijing = [[UIImageView alloc] initWithFrame:self.view.bounds];
    beijing.image = [UIImage imageNamed:@"beijing"];
    [self.view addSubview:beijing];
  
    //用户名输入框
    yhmk = [[UITextField alloc] init];
    yhmk.frame = CGRectMake(93, 200, 175, 40);
    yhmk.font = [UIFont boldSystemFontOfSize:20];
    yhmk.textColor = [UIColor whiteColor];
    NSAttributedString *yhma = [[NSAttributedString alloc] initWithString:@"用户名"attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:yhmk.font}];
    yhmk.attributedPlaceholder = yhma;
    yhmk.textAlignment = NSTextAlignmentCenter;
    
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(90,240, 180, 2)];
    line.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:line];
    
    //密码输入框
    mmk = [[UITextField alloc] init];
    mmk.frame = CGRectMake(93, 260, 175, 40);
    mmk.font = [UIFont boldSystemFontOfSize:20];
    mmk.textColor = [UIColor whiteColor];
    NSAttributedString *mma = [[NSAttributedString alloc] initWithString:@"密码"attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:mmk.font}];
    mmk.textAlignment = NSTextAlignmentCenter;
    mmk.attributedPlaceholder = mma;
    mmk.secureTextEntry = YES ;
    UIView *linem=[[UIView alloc]initWithFrame:CGRectMake(90,300, 180, 2)];
    linem.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:linem];
    //登录按钮
    login = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //创建按钮
    login.frame = CGRectMake(135, 350, 80, 40);
    [login.layer setMasksToBounds:YES];
    [login.layer setCornerRadius:20.0]; //设置矩形四个圆角半径
    [self->login.layer setBorderColor:[UIColor whiteColor].CGColor];
    [login setFont: [UIFont boldSystemFontOfSize:14]];
    //边框宽度
    [login.layer setBorderWidth:1.0];
    [login setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
    [login setTitle:@"登录" forState:UIControlStateNormal];
    //向按钮添加事件函数
    [login addTarget:self action:@selector(presslogin) forControlEvents:UIControlEventTouchUpInside];
   
    //注册按钮
    zhuce = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    zhuce.frame = CGRectMake(135, 400, 80, 40);
    [zhuce.layer setMasksToBounds:YES];
    [zhuce.layer setCornerRadius:20.0]; //设置矩形四个圆角半径
    [self->zhuce.layer setBorderColor:[UIColor whiteColor].CGColor];
    [zhuce setFont: [UIFont boldSystemFontOfSize:14]];
    //边框宽度
    [zhuce.layer setBorderWidth:1.0];
    [zhuce setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
    [zhuce setTitle:@"注册" forState:UIControlStateNormal];
    [zhuce addTarget:self action:@selector(presszhuce) forControlEvents:UIControlEventTouchUpInside];
   
    
    //将所有的控件添加到视图中显示
    [self.view addSubview:yhm];
    [self.view addSubview:mm];
    [self.view addSubview:yhmk];
    [self.view addSubview:mmk];
    [self.view addSubview:login];
    [self.view addSubview:zhuce];

    
}
//设置背景图片
-(void) beijing
{
    UIImageView *beijing = [[UIImageView alloc] initWithFrame:self.view.bounds];
    beijing.image = [UIImage imageNamed:@"beijing"];
    [self.view addSubview:beijing];
}


//登录事件函数
-(void) presslogin
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setValue:yhmk.text forKey:@"tel"];
    [dict setValue:mmk.text forKey:@"password"];
    NSString *urlStr = [NSString stringWithFormat:@"http://172.24.10.175/workout/api.php/login"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:urlStr parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject[@"error"]) {
        //这里需要提示
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"帐号或密码错误";
            hud.margin = 10.f;
            hud.yOffset = 150.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:3];
        }else{
            NSUserDefaults *userdefa = [NSUserDefaults standardUserDefaults];
            [userdefa setObject:responseObject[@"id"] forKey:@"userID"];
        TabBarViewController *tabVC = [[TabBarViewController alloc] init];
        [self presentViewController:tabVC animated:YES completion:nil];
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
    }];
    

  
}
//注册事件函数
-(void)presszhuce
{
   ZhuceViewController *zhuceVC = [[ZhuceViewController alloc] init];
    [self presentViewController:zhuceVC animated:YES completion:nil];
}


-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //回收键盘
    [yhmk resignFirstResponder];
    [mmk resignFirstResponder];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end

